package com.eascs.p2p.investor.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.p2p.common.dao.BaseDao;
import com.eascs.p2p.common.entity.InvestorEntity;
import com.eascs.p2p.common.entity.InvestorRecordEntity;
import com.eascs.p2p.common.security.MD5;
import com.eascs.p2p.common.vo.InvestorVO;
import com.eascs.p2p.investor.dao.InvestorDao;

import jodd.util.StringUtil;

@Repository
public class InvestorDaoImpl extends BaseDao implements InvestorDao {
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Override
	public InvestorEntity queryInvestor(InvestorEntity investor) {
		List<Object> param = new ArrayList<>();
		StringBuffer sql = new StringBuffer();
		sql.append(" select * from p2p_investor where 1=1");
		if (StringUtils.isNoneBlank(investor.getAccount())) {
			param.add(investor.getAccount());
			sql.append(" and account=?");
		}
		if (StringUtils.isNoneBlank(investor.getPassword())) {
			param.add(investor.getPassword());
			sql.append(" and password=?");
		}
		if (StringUtils.isNoneBlank(investor.getId())) {
			param.add(investor.getId());
			sql.append(" and id=?");
		}
		return this.findEntity(sql.toString(), InvestorEntity.class, param.toArray());
	}

	@Override
	public InvestorEntity insertInvestor(InvestorEntity investor) {
		String pw = investor.getPassword();
		if (StringUtils.isNotBlank(pw)) {
			pw = pw.trim();
			investor.setPassword(MD5.md5Encode(pw));
		}
		investor.setCreateTime(new Date());
		investor.setUpdateTime(new Date());
		this.p2pSession.insert(investor);
		return investor;
	}
	

	@Override
	public Integer insertInvestorRecord(InvestorRecordEntity investorRecord) {
		investorRecord.setCreateTime(new Date());
		investorRecord.setUpdateTime(new Date());
		return this.p2pSession.insert(investorRecord);
	}

	@Override
	public List<InvestorRecordEntity> queryInvestorRecordListByInvestObjId(String investObjId) {
		String sql = "select * from p2p_investor_record where object_id=?";
		return this.p2pSession.findEntitys(sql, InvestorRecordEntity.class, investObjId);
	}

	@Override
	public List<InvestorRecordEntity> queryInvestorRecordList(InvestorRecordEntity investorRecord) {
		List<Object> param = new ArrayList<>();
		StringBuffer sql = new StringBuffer();
		sql.append("select * from p2p_investor_record where 1=1");
		if (StringUtils.isNoneBlank(investorRecord.getObjectId())) {
			param.add(investorRecord.getObjectId());
			sql.append(" and object_id=?");
		}
		if (StringUtils.isNoneBlank(investorRecord.getInvestorId())) {
			param.add(investorRecord.getInvestorId());
			sql.append(" and investor_id=?");
		}
		if (-1 != investorRecord.getStatus()) {
			param.add(investorRecord.getStatus());
			sql.append(" and status=?");
		}
		return this.p2pSession.findEntitys(sql.toString(), InvestorRecordEntity.class, param.toArray());
	}

	@Override
	public PageResult<Object[]> findPageInvestorEntity(InvestorVO vo) {

		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, vo.getPageSize(), vo.getShowPage());
		StringBuilder sql = new StringBuilder();
		sql.append(
				"SELECT investor.id, investor. name, investor.mobile,investor.id_card_no,bank.bank_no, item.item_name AS channel, "
				+ " investor.create_time, investor.last_login_time, (wallet.current_balance + wallet.invest_frozen + wallet.withdraw_frozen + wallet.await_principal) as total,wallet.current_balance, wallet.accumulate_vote, wallet.accumulate_income, CASE WHEN (investor. STATUS = 0) THEN '启用' WHEN (investor. STATUS = 1) THEN '停用' END AS STATUS FROM p2p_investor investor LEFT JOIN p2p_datadic_item item ON investor.channel = item.item_code AND item.dd_no = 3 LEFT JOIN p2p_investor_wallet wallet ON wallet.investor_id = investor.id "
				+ " left join p2p_investor_bank bank on investor.bank_requestno=bank.id WHERE 1 = 1");
		// 姓名
		if (StringUtil.isNotBlank(vo.getName())) {
			sql.append(" and investor.name like ?");
			sqlPageQuery.addParam("%" + vo.getName() + "%");
		}

		// 手机号
		if (StringUtil.isNotBlank(vo.getMobile())) {
			sql.append(" and investor.mobile like ?");
			sqlPageQuery.addParam("%" + vo.getMobile() + "%");
		}
		// 来源
		if (vo.getChannel() > 0) {
			sql.append(" and investor.channel = ?");
			sqlPageQuery.addParam(vo.getChannel());
		}
		// 状态
		if (StringUtil.isNotBlank(vo.getStatus())) {

			sql.append(" and investor.status = ?");
			sqlPageQuery.addParam(vo.getStatus());
		}

		// 创建时间
		if (StringUtil.isNotBlank(vo.getBeginCreateTime())) {

			sql.append(" and date_format(investor.create_time,'%Y-%m-%d') >= ?");
			sqlPageQuery.addParam(vo.getBeginCreateTime());
		}
		if (StringUtil.isNotBlank(vo.getEndCreateTime())) {

			sql.append(" and date_format(investor.create_time,'%Y-%m-%d') <= ?");
			sqlPageQuery.addParam(vo.getEndCreateTime());
		}
		
		
		sql.append(" and is_del= 0 order by investor.create_time desc ");
		sqlPageQuery.setQuerySql(sql.toString());
		PageResult<Object[]> page = this.findDataWithPage(sqlPageQuery);
		return page;
	}

	@Override
	public List<Object[]> getInvestorNameorMobile(String keywords) {

		List<Object[]> dataSouceList = new ArrayList<>();
		StringBuffer sql = new StringBuffer("select id,name,mobile from p2p_investor t  where 1=1");
		sql.append(" and name like ?");
		sql.append(" or mobile  like ?");
		sql.append(" order by id ");
		sql.append(" limit 0,10 ");
		dataSouceList = this.findObjectsList(sql.toString(), "%" + keywords + "%", "%" + keywords + "%");
		return dataSouceList;
	}

	@Override
	public boolean updateStatus(String id, int status) {
		String sql = "update p2p_investor set status=? where id=?";
		return this.execute(sql, status, id);

	}

	@Override
	public boolean savePasswrod(String id, String password) {
		String sql = "update p2p_investor set trade_password=? where id=?";
		return this.execute(sql, password, id);
	}

	@Override
	public String getPasswrod(String id) {
		String sql = "select trade_password from  p2p_investor where id = ? ";
		return this.findString(sql, id);
	}

	@Override
	public boolean updateInvestor(InvestorEntity investor) {
		List<Object> param = new ArrayList<>();
		StringBuffer sql = new StringBuffer();
		sql.append("update p2p_investor set ");
		if (StringUtils.isNotBlank(investor.getName())) {
			sql.append(" name=?,");
			param.add(investor.getName());
		}
		if (StringUtils.isNotBlank(investor.getNickname())) {
			sql.append(" nickname=?,");
			param.add(investor.getNickname());
		}
		if (StringUtils.isNotBlank(investor.getPassword())) {
			sql.append(" password=?,");
			param.add(investor.getPassword());
		}
		if (StringUtils.isNotBlank(investor.getTradePassword())) {
			sql.append(" trade_password=?,");
			param.add(investor.getTradePassword());
		}
		if (investor.getIsRealname() != 0) {
			sql.append(" is_realname=?,");
			param.add(investor.getIsRealname());
		}
		if (StringUtils.isNotBlank(investor.getMobile())) {
			sql.append(" mobile=?,");
			param.add(investor.getMobile());
		}
		if (StringUtils.isNotBlank(investor.getBankRequestno())) {
			sql.append(" bank_requestno=?,");
			param.add(investor.getBankRequestno());
		}
		if (StringUtils.isNotBlank(investor.getIdCardNo())) {
			sql.append(" id_card_no=?,");
			param.add(investor.getIdCardNo());
		}
		if (investor.getIsDel() != 0) {
			sql.append(" is_del=?,");
			param.add(investor.getIsDel());
		}
		if (investor.getStatus() != 0) {
			sql.append(" status=?,");
			param.add(investor.getStatus());
		}
		if (StringUtils.isNotBlank(investor.getLastLoginIp())) {
			sql.append(" last_login_ip=?,");
			param.add(investor.getLastLoginIp());
		}
		if (StringUtils.isNotBlank(investor.getLastLoginMac())) {
			sql.append(" last_login_mac=?,");
			param.add(investor.getLastLoginMac());
		}
		if (investor.getLastLoginTime() != null) {
			sql.append(" last_login_time=?,");
			param.add(investor.getLastLoginTime());
		}
		sql.append(" update_time=?");
		param.add(new Date());
		sql.append(" WHERE id=?");
		param.add(investor.getId());
		return this.execute(sql.toString(), param.toArray());
	}

	@Override
	public boolean updateInvestorRecord(InvestorRecordEntity investorRecord) {
		List<Object> param = new ArrayList<>();
		StringBuffer sql = new StringBuffer();
		sql.append("update p2p_investor_record set ");
		if (investorRecord.getStatus() != -1) {
			sql.append(" status=?,");
			param.add(investorRecord.getStatus());
		}
		sql.append(" update_time=?");
		param.add(new Date());
		sql.append(" WHERE id=?");
		param.add(investorRecord.getId());
		return this.execute(sql.toString(), param.toArray());

	}
	
	@Override
	public InvestorEntity queryInvestorByAccount(String account) {
		String sql = "select * from p2p_investor where account=? limit 1";
		return this.findEntity(sql, InvestorEntity.class, account);
	}
}
